import webapp2

import models
import handlers
import gviz_api
import json
from google.appengine.api import memcache

ONE_DAY = 86400

class TravelWarningsHandler(webapp2.RequestHandler):
    def get(self):
        description = {
                       "country": ("string", "Country"),
                       "warnings": ("string", "Travel Warnings"),
                       "info": ("string", "Information"),
                       "href": ("string", "URL")
                       }
        data = []
        
        query = models.Country.all()
        query.filter("travel_warning >", "")
        for country in query:
            data.append({
                         "country":country.iso_a2,
                         "warnings":1,
                         "info":country.name,
                         "href":"http://travel.state.gov" + country.travel_warning
                         })
        data_table = gviz_api.DataTable(description)
        data_table.LoadData(data)
        
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write(data_table.ToJSonResponse(
                                                          ("country", "warnings", "info", "href"),
                                                          "country"))
        
class VaccineRecommendationsHandler(webapp2.RequestHandler):
    def get(self):
        vaccine = self.request.get('vaccine')
        tqx = self.request.get('tqx')
        
        description = {
                       "country": ("string", "Country"),
                       "recommendation": ("string", "Recommendation"),
                       "info": ("string", "Information"),
                       "text": ("string", "Text")
                       }
        data = []
        query = models.VaccineRecommendation.all()
        query.filter("vaccine =", vaccine)
        for recommendation in query:
            #self.response.out.write(recommendation.country.name + "\n")
            data.append({
                         "country":recommendation.country.iso_a2,
                         "recommendation":1,
                         "info":recommendation.country.name,
                         "text":recommendation.text
                         })
        data_table = gviz_api.DataTable(description)
        data_table.LoadData(data)
        
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write(data_table.ToResponse(
                                                          ("country", "recommendation", "info", "text"),
                                                          "country",
                                                          tqx=tqx))
        
class VaccineTypesHandler(webapp2.RequestHandler):
    def get(self):
        vaccines = memcache.get("vaccine_types")
        if not vaccines:
            vaccines = set()
            query = models.VaccineRecommendation.all()
            for recommendation in query:
                vaccines.add(recommendation.vaccine)
            vaccines = list(sorted(vaccines))
            memcache.add("vaccine_types", vaccines, ONE_DAY)
        
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write(json.dumps(vaccines))
        
travelwarnings_handler = webapp2.WSGIApplication([('/data/travelwarnings', TravelWarningsHandler)], debug=True)
vaccinerecommendations_handler = webapp2.WSGIApplication([('/data/vaccinerecommendations', VaccineRecommendationsHandler)], debug=True)
vaccinetypes_handler = webapp2.WSGIApplication([('/data/vaccinetypes', VaccineTypesHandler)], debug=True)
